<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.device_no" placeholder="设备编号"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="handleEdit" :loading="queryLoading">查询</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table ref="singleTable" :data="devices" border highlight-current-row v-loading="listLoading" style="width: 100%;" @row-click="selectRow">
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="device_no" label="设备编号" width="140" sortable>
			</el-table-column>
			<el-table-column prop="device_type" label="类型" width="100" :formatter="formatType" sortable>
			</el-table-column>
			<el-table-column prop="uid" label="所属用户" width="140" sortable>
			</el-table-column>
			<el-table-column prop="status" label="状态" width="100" :formatter="formatStatus" sortable>
			</el-table-column>
			<el-table-column prop="critical_value1" label="pm2.5上限" width="140">
			</el-table-column>
			<el-table-column prop="critical_value2" label="pm2.5下限" width="140">
			</el-table-column>
			<el-table-column prop="address" label="安装地址" min-width="220">
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="30" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog title="详情" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
				<el-form-item label="设备编号" prop="device_no">
					<el-input v-model="editForm.device_no" auto-complete="off" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item label="设备类型" prop="device_type">
					<el-select v-model="editForm.device_type" placeholder="请选择" :disabled="true">
						<el-option v-for="item in typeEmu" :key="item.key" :label="item.value" :value="item.key">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="所属用户" prop="uid">
					<el-input v-model="editForm.uid" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item label="设备状态" prop="status">
					<el-select v-model="editForm.status" placeholder="请选择" :disabled="true">
						<el-option v-for="item in statusEmu" :key="item.key" :label="item.value" :value="item.key">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="安装地址" prop="address">
					<el-input v-model="editForm.address" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="安装时间" prop="created_at">
					<el-input v-model="editForm.created_at" :disabled="true"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm2.5上限" prop="critical_value1">
					<el-input v-model="editForm.critical_value1"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm2.5下限" prop="critical_value2">
					<el-input v-model="editForm.critical_value2"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm10上限" prop="critical_value3">
					<el-input v-model="editForm.critical_value3"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm10下限" prop="critical_value4">
					<el-input v-model="editForm.critical_value4"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm100上限" prop="critical_value5">
					<el-input v-model="editForm.critical_value5"></el-input>
				</el-form-item>
				<el-form-item style="width:240px" label="pm100下限" prop="critical_value6">
					<el-input v-model="editForm.critical_value6"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交更改</el-button>
				<el-button type="primary" @click.native="editFormVisible = false">确定</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
export default {
	data() {
		return {
			filters: {
				device_no: '',
				uid: ''
			},
			devices: [],
			total: 0,
			page: 1,
			listLoading: false,
			queryLoading: false,

			//编辑界面
			editFormVisible: false,
			editLoading: false,
			editFormRules: {
				critical_value1: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value2: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value3: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value4: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value5: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				],
				critical_value6: [
					{ pattern: /^\d+(\.\d+)?$/, message: '请输入正确的数值', trigger: 'blur' }
				]
			},
			editForm: {
				device_no: '',
				status: '01',
				type: '01',
				uid: '',
				address: '',
				critical_value1: null,
				critical_value2: null,
				critical_value3: null,
				critical_value4: null,
				critical_value5: null,
				critical_value6: null,
				created_at: ''
			},
			statusEmu: [
				{
					key: '00',
					value: '禁用'
				}, {
					key: '01',
					value: '正常'
				}, {
					key: '02',
					value: '故障'
				}
			],
			typeEmu: [
				{
					key: '01',
					value: '监控'
				}, {
					key: '02',
					value: '其他'
				}
			]
		}
	},
	methods: {
		formatType: function(row, column) {
			for (let i = 0; i < this.typeEmu.length; i++) {
				if (row.device_type == this.typeEmu[i].key) {
					return this.typeEmu[i].value
				}
			}
			return '未知类型'
		},
		formatStatus: function(row, column) {
			for (let i = 0; i < this.statusEmu.length; i++) {
				if (row.status == this.statusEmu[i].key) {
					return this.statusEmu[i].value
				}
			}
			return '未知类型'
		},
		handleCurrentChange: function(val) {
			this.page = val
			this.getDevices()
		},
		//获取设备列表
		getDevices: function() {
			this.listLoading = true

			let token = sessionStorage.getItem('token')
			let params = this.formatHttpGetParams({ page: this.page, size: 30, token: token })
			let path = 'api/devices/list/' + this.filters.uid
			this.$http.get(this.$store.state.domain + path + params).then(function(res) {
				if (res.body.respCode == '0000') {
					this.total = res.body.count
					this.devices = res.body.data
				} else {
					if (res.body.respMsg != null) {
						alert(res.body.respMsg)
					} else {
						alert('后台未知异常，请与管理员联系')
					}
				}
				this.listLoading = false
			}, function() {
				alert('网络请求处理失败')
				this.listLoading = false
			})
		},
		//显示编辑界面
		handleEdit: function() {
			if (this.filters.device_no) {
				this.queryLoading = true
				//通过设备编号获取设备信息
				let token = sessionStorage.getItem('token')
				let params = this.formatHttpGetParams({ token: token })
				let path = 'api/devices/query/' + String.trim(this.filters.device_no)
				this.$http.get(this.$store.state.domain + path + params).then(function(res) {
					if (res.body.respCode == '0000') {
						if (res.body.data.device_no) {
							this.editForm = res.body.data
							this.editFormVisible = true
						} else {
							alert('未找到相关设备信息')
						}
					} else {
						if (res.body.respMsg != null) {
							alert(res.body.respMsg)
						} else {
							alert('后台未知异常，请与管理员联系')
						}
					}
					this.queryLoading = false
				}, function() {
					alert('网络请求处理失败')
					this.queryLoading = false
				})
			} else {
				alert('请输入设备编号')
			}
		},
		//编辑
		editSubmit: function() {
			this.$refs.editForm.validate((valid) => {
				if (valid) {
					this.$confirm('确认提交吗？', '提示', {}).then(() => {
						this.editLoading = true

						let data = new Object()
						data.device_no = this.editForm.device_no
						data.status = null
						data.uid = null
						data.address = null
						data.critical_value1 = parseFloat(this.editForm.critical_value1)
						data.critical_value2 = parseFloat(this.editForm.critical_value2)
						data.critical_value3 = parseFloat(this.editForm.critical_value3)
						data.critical_value4 = parseFloat(this.editForm.critical_value4)
						data.critical_value5 = parseFloat(this.editForm.critical_value5)
						data.critical_value6 = parseFloat(this.editForm.critical_value6)

						let token = sessionStorage.getItem('token')
						let params = this.formatHttpGetParams({ token: token })
						let path = 'api/devices/update'
						this.$http.post(this.$store.state.domain + path + params, JSON.stringify(data), { emulateJSON: true })
							.then(function(res) {
								if (res.body.respCode == '0000') {
									alert('修改成功！')
									this.$refs['editForm'].resetFields()
									this.editFormVisible = false
									this.editLoading = false
									this.getDevices()
								} else {
									if (res.body.respMsg != null) {
										alert(res.body.respMsg)
									} else {
										alert('后台未知异常，请与管理员联系')
									}
									this.$refs['editForm'].resetFields()
									this.editLoading = false
								}
							},
							function(error) {
								alert('网络请求处理失败')
								this.$refs['editForm'].resetFields()
								this.editLoading = false
							})
					})
				}
			})
		},
		selectRow: function(row, event, column) {
			this.$router.push({ path: '/latest-data', query: { device_no: row.device_no } });
		}
	},
	mounted() {
		var user = sessionStorage.getItem('user')
		if (user) {
			user = JSON.parse(user)
			this.filters.uid = user.uid
		}
		this.getDevices()
	}
}
</script>

<style scoped>

</style>